<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginData"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2 style="margin: 15px 0px">欢迎来到Real甄选运营平台</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginData.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              v-model="loginData.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              @click="login"
              class="login_btn"
              type="primary"
              size="default"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { ElNotification } from 'element-plus'
// 引入用户相关的小仓库
import useUserStore from '@/store/modules/user.ts'
// 引入路由
import { useRouter, useRoute } from 'vue-router'
// 引入获取时间的函数
import { getTime } from '../../utils/time'
// 获取el-form组件
const loginForms = ref()
// 创建路由器对象
const router = useRouter()
// 创建路由对象
const route = useRoute()
// 创建一个user仓库实例
const userStore = useUserStore()
// 收集账号与密码的数据
let loginData = reactive({ username: 'admin', password: 'atguigu123' })
// 定义一个变量控制加载效果
let loading = ref(false)

// 点击登录按钮的回调
const login = async () => {
  // 在表单校验完成之后再发送请求
  await loginForms.value.validate()
  loading.value = true
  // 要明白点击登录之后要做一些什么
  // 1、通知仓库发送登录请求
  // 2、请求成功要跳转到主页面
  // 3、失败就要提示失败的信息
  try {
    // console.log(useUserStore);
    // 调用仓库方法发送登录请求
    await userStore.userLogin(loginData)
    // 请求成功跳转到主页面
    // 判断登录的时候，路由路径当中是否存在query参数  获取当前路径的query.redirect参数
    const redirect: any = route.query.redirect
    router.push({ path: redirect || '/' })
    // 登录信息提示
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `Hi,${getTime()}好`,
    })
    // 修改loading
    loading.value = false
  } catch (error) {
    // 修改loading
    loading.value = false
    // 失败信息提示
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}

// 自定义表单校验函数
const validatorUserName = (rule: any, value: any, callback: any) => {
  // rule是表单校验的规则
  // value是表单的文本值
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('用户名至少要五位'))
  }
}

const validatorPassword = (rule: any, value: any, callback: any) => {
  // rule是表单校验的规则
  // value是表单的文本值
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码至少要六位'))
  }
}

// 定义表单校验的对象
const rules = {
  // username: [{ required: true, min: 5, message: '用户名的长度是5-16位', max: 16, trigger: 'change' }],
  username: [{ trigger: 'change', validator: validatorUserName }],
  // password: [{ required: true, min: 5, message: '密码的长度是5-16位', max: 16, trigger: 'change' }]
  password: [{ trigger: 'change', validator: validatorPassword }],
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  // background-color: aqua;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
}

.login_form {
  position: relative;
  width: 80%;
  top: 25vh;
  background: url('@/assets/images/login_form.png') no-repeat;
  background-size: cover;
  padding: 40px;
}

h1 {
  color: white;
  font-size: 30px;
  margin-bottom: 10px;
}

h2 {
  color: white;
  font-size: 20px;
  margin-bottom: 10px;
}

.login_btn {
  width: 100%;
}
</style>
